import { httpClient } from '@/utils/http/axios';
import { ErrorMessageMode } from '#/axios';
import { AjaxResult } from '@/api/model';
import { PageRequest, PageData } from '@/utils/common/types';
import { InputDto, OutputDto } from './ipn.data';

enum Api {
  Get = '/api/admin/ipn/get',
  Read = '/api/admin/ipn/read',
  ReadIPN = '/api/admin/ipn/readipn',
  Create = '/api/admin/ipn/create',
  Update = '/api/admin/ipn/update',
  Delete = '/api/admin/ipn/delete',
  GetMaterialTreeNodes = '/api/admin/materialcategory/gettreenodes',
  GetDictNodes = '/api/dict/getdicts',
  ReadTreeNode = '/api/admin/materialonelevel/readtreenode',
  ReadOneLevelNodes = '/api/admin/materialonelevel/readnodes',
  ApplyWaitToRd = '/api/admin/ipn/applywaittord',
  ApplyRdToIpn = '/api/admin/ipn/applyrdtoipn',
  ApplyRdToWait = '/api/admin/ipn/applyrdtowait',
  ApplyIpnToRd = '/api/admin/ipn/applyipntord',
  ApplyIpnToComplete = '/api/admin/ipn/applyipntocomplete',
}

export function Get(ipnNo: string, mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Get,
      params: { ipnNo },
    },
    {
      joinParamsToUrl: true,
      errorMessageMode: mode,
    },
  );
}

export function Read(request: PageRequest, mode: ErrorMessageMode = 'message') {
  return httpClient.post<PageData<OutputDto>>(
    {
      url: Api.Read,
      params: request,
    },
    {
      errorMessageMode: mode,
    },
  );
}

export function ReadIPN(request: PageRequest, mode: ErrorMessageMode = 'message') {
  return httpClient.post<PageData<OutputDto>>(
    {
      url: Api.ReadIPN,
      params: request,
    },
    {
      errorMessageMode: mode,
    },
  );
}

export function Create(dtos: InputDto[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Create,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}

export function Update(dtos: InputDto[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Update,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}

export function Delete(ids: string[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Delete,
      params: ids,
    },
    {
      errorMessageMode: mode,
    },
  );
}

export function ApplyWaitToRd(id: string, mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.ApplyWaitToRd,
      params: { id },
    },
    {
      joinParamsToUrl: true,
      errorMessageMode: mode,
    },
  );
}

export function ApplyRdToIpn(id: string, mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.ApplyRdToIpn,
      params: { id },
    },
    {
      joinParamsToUrl: true,
      errorMessageMode: mode,
    },
  );
}

export function ApplyRdToWait(id: string, mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.ApplyRdToWait,
      params: { id },
    },
    {
      joinParamsToUrl: true,
      errorMessageMode: mode,
    },
  );
}

export function ApplyIpnToRd(id: string, mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.ApplyIpnToRd,
      params: { id },
    },
    {
      joinParamsToUrl: true,
      errorMessageMode: mode,
    },
  );
}

export function ApplyIpnToComplete(dto: InputDto, mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.ApplyIpnToComplete,
      params: dto,
    },
    {
      errorMessageMode: mode,
    },
  );
}

export function GetMaterialTreeNodes(mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.GetMaterialTreeNodes,
      params: {},
    },
    {
      errorMessageMode: mode,
    },
  );
}

export function ReadOneLevelNodes(mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.ReadOneLevelNodes,
      params: {},
    },
    {
      errorMessageMode: mode,
    },
  );
}

export function GetDictNodes(code: string, mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.GetDictNodes,
      params: { code },
    },
    {
      joinParamsToUrl: true,
      errorMessageMode: mode,
    },
  );
}
